package Demo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-01-05
 * Time: 16:17
 */
public class Thread_Demo10 {
    public static void main(String[] args) throws InterruptedException {
        // 三个线程顺序打印 ABC
        Object lock1 = new Object();
        Object lock2 = new Object();

        Thread t1 = new Thread(() -> {
            System.out.println("A");

            synchronized (lock1) {
                lock1.notify();
            }
        });

        // 若先执行了t1 的notify 会导致后续t2对t1的等待无反应

        Thread t2 = new Thread(() -> {
            synchronized (lock1) {
                try {
                    lock1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("B");

            synchronized (lock2) {
                lock2.notify();
            }

        });

        //如果先执行了 t3的notify 会导致后续t3对t2的等待无反应

        Thread t3 = new Thread(() -> {
            synchronized (lock2) {
                try {
                    lock2.wait();
                    System.out.println("C");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });

        t2.start();
        t3.start();
        Thread.sleep(1000);  // 保证t2 和t3 先执行wait t1在notify
        t1.start();

    }
}
